package com.example.supernote.model.note.commands.Text

import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.example.supernote.R
import com.example.supernote.model.note.commands.AbstractCommand
import com.example.supernote.model.note.commands.insertTextAtRange
import com.example.supernote.model.note.context.Context

// 标题
class H1Command : AbstractCommand(R.string.note_H1) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("# ", "")
        )


    override fun undo(context: Context) {
        println(stringResId)
    }
}

class H2Command : AbstractCommand(R.string.note_H2) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("## ", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class H3Command : AbstractCommand(R.string.note_H3) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("### ", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class H4Command : AbstractCommand(R.string.note_H4) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("#### ", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class H5Command : AbstractCommand(R.string.note_H5) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("##### ", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class H6Command : AbstractCommand(R.string.note_H6) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("###### ", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

// 字体样式
class BoldCommand: AbstractCommand(R.string.note_bolder) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("**", "**")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class ItalicCommand: AbstractCommand(R.string.note_italic) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("*", "*")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class DeleteLineCommand: AbstractCommand(R.string.note_deleteline) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("~~", "~~")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class UnderLineCommand: AbstractCommand(R.string.note_underline) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("<u>", "</u>")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class FontColorCommand: AbstractCommand(R.string.note_font_color) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("<font color='${context.generateText}'>", "</font>")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class FontSizeCommand: AbstractCommand(R.string.note_font_size) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("<font size='${context.generateText}'>", "</font>")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

// ai+
class ExtractOutlineCommand: AbstractCommand(R.string.note_extract_outline) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = TextRange(0, 1),
            insertText = listOf("\n" + context.generateText + "\n", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class ExtractSummaryCommand: AbstractCommand(R.string.note_extract_summary) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = TextRange(0, 1),
            insertText = listOf("\n" + context.generateText + "\n", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class PolishTextCommand: AbstractCommand(R.string.note_polish_text) {
    override fun execute(context: Context): TextFieldValue {
        val currentText = context.article.text
        val textRange = context.selection
        val start = textRange.min
        val newText = buildString {
            append(currentText.substring(0, start))
            append(currentText.substring(textRange.max))
        }
        return insertTextAtRange(
            currentTextFieldValue = TextFieldValue(newText),
            textRange = TextRange(start, start),
            insertText = listOf("\n" + context.generateText + "\n", "")
        )
    }

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class TranslateTextCommand: AbstractCommand(R.string.note_translation) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("\n" + context.generateText + "\n\n", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}

class TextGenerateCommand: AbstractCommand(R.string.note_text_generate) {
    override fun execute(context: Context): TextFieldValue =
        insertTextAtRange(
            currentTextFieldValue = context.article,
            textRange = context.selection,
            insertText = listOf("\n" + context.generateText + "\n\n", "")
        )

    override fun undo(context: Context) {
        println(stringResId)
    }
}